package JDBCUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.*;

public class LOGIN extends JFrame {

    // 定义组件
    ImageIcon background = new ImageIcon("D:\\IDEA.flle\\JDBC\\Test\\src\\JDBCUI\\image.jpg");
    JLabel label = new JLabel(background);

    JPanel jp1, jp2, jp3;
    JLabel jlb1, jlb2;
    JButton jlogin, jclose;
    JTextField jtf1;
    JPasswordField jpf1;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LOGIN d1 = new LOGIN();
    }

    // 构造函数
    public LOGIN() {

        // 可以发现按钮和文本框都已经有了变化，给窗口润色不少。
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }catch(Exception e) {
            System.out.println(e);
        }

        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();

        jlb1 = new JLabel("用 户 名 :");

        jlb2 = new JLabel("密    码 :");

        jlogin = new JButton(" 登 录 ");

        jclose = new JButton(" 取 消 ");

        jtf1 = new JTextField(10);     this.setLayout(new GridLayout(3, 2));

        jpf1 = new JPasswordField(10);
        // 设置布局管理
       this.setLayout(new FlowLayout(FlowLayout.CENTER,50,60));


        // 加入各个组件
        jp1.add(jlb1);
        jp1.add(jtf1);
        jp1.setOpaque(false);

        jp2.add(jlb2);
        jp2.add(jpf1);
        jp2.setOpaque(false);

        jp3.add(jlogin);
        jp3.add(jclose);
        jp3.setOpaque(false);

        // 加入到JFrame
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);



     jclose.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent evt) {
             dispose();
         }
     });



        jlogin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {


                //JDBC
                Connection conn= null;
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    //1、注册驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");

                    //2、获取连接
                    conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");

                    //3、创建数据库操作对象
                    stmt=conn.createStatement();

                    String usename = jtf1.getText();
                    String  passward =jpf1.getText();

                    //4、执行sql语句
                    String sql="select * from user where username = '"+usename+"' and passward = '"+passward+"'  ";
                    rs = stmt.executeQuery(sql);

                    //5、处理查询结果集
                    if (rs.next()){
                        //登陆成功
                        javax.swing.JOptionPane.showMessageDialog(null, "登录成功!");
                        dispose();
                        new MainJFrame();
                    }else {
                        javax.swing.JOptionPane.showMessageDialog(null, "登录失败！!");
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    //6、释放资源，从小到大
                    if ( conn!= null ){
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    if ( stmt!= null ){
                        try {
                            stmt.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    if ( rs!= null ){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }

                }

            }
        });



        /*
         * 设置标签大小
         */
        label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        /*
         * 主界面获得容器JPanel
         */
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        JPanel jp = (JPanel) this.getContentPane();
        /*
         * JPanel对象才可以调用setOpaque(false);设置是否透明
         */

       jp.setOpaque(false);
        JPanel jpanel = new JPanel();
        jpanel.setOpaque(false);




        this.setLocation(800,300);
        this.setSize(400,400);
        this.setTitle("  你 好，世 界 ");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}